package com.lianzhihui.romeex.ui.me;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hyphenate.helpdesk.easeui.util.UserUtil;
import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.bean.BindExchangeBean;
import com.lianzhihui.romeex.bean.ExchangeFindHomeBean;
import com.lianzhihui.romeex.bean.FindExchangeInfoBean;
import com.lianzhihui.romeex.bean.UserInfoBean;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.mvp.presenter.BindRomeexPresnterImp;
import com.lianzhihui.romeex.mvp.view.BindRomeexView;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.ui.me.dialog.VerificationCodeDialog;
import com.lianzhihui.romeex.utils.IntentUtils;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.PreferenceUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.utils.UserUtils;

import java.text.MessageFormat;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me
 * @ClassName: 绑定交易所
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/2 15:12
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/2 15:12
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class BindRomeexActivity extends BaseActivity implements BindRomeexView {

    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.unbind_layout)
    LinearLayout unbindLayout;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.hint_title)
    TextView hintTitle;
    @BindView(R.id.tv_submit)
    TextView tvSubmit;
    @BindView(R.id.et_uid)
    EditText etUid;
    @BindView(R.id.et_input_phone)
    EditText etInputPhone;
    @BindView(R.id.iv_user_image)
    ImageView ivUserImage;
    @BindView(R.id.tv_romexx)
    TextView tvRomexx;
    @BindView(R.id.user_uid)
    TextView userUid;
    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.icon_binded_layout)
    RelativeLayout iconBindedLayout;
    private BindRomeexPresnterImp bindRomeexPresnterImp;
    private String exchangeUid = "";
    private String uidQuota = "";

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_romeex;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTransparent(this);
        StatusBarUtil.setStatusBarTextColor(this, false);
        setTitle(getResources().getString(R.string.text_bind_romeex));
    }

    @Override
    protected void initData() {

        Bundle extras = getIntent().getExtras();
        if (null != extras) {
            exchangeUid = extras.getString(AppConstants.EXCHANGEUID, "");
        }
        if (exchangeUid.isEmpty()) {
            //未绑定
            LogUtil.error("xxxxxx:", UserUtils.getCurrentUserExchangeUid());
            if (!UserUtils.getCurrentUserExchangeUid().isEmpty()) {
                bindRomeex();
            }
        } else {
            //已绑定
            bindRomeex();
        }
        bindRomeexPresnterImp = new BindRomeexPresnterImp(this, this);
        bindRomeexPresnterImp.requestExchangeFindHome();
    }


    @OnClick({R.id.include_top_iv_back, R.id.tv_submit, R.id.include_top_tv_right, R.id.tv_balance})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {
                case R.id.include_top_iv_back:
                    finish();
                    break;
                case R.id.include_top_tv_right:
                    if (!uidQuota.isEmpty()) {
                        IntentUtils.JumpToHaveOne(CreditRecordActivity.class, AppConstants.INTENT_EXCHANGE_BALANCE, uidQuota);
                    }
                    break;
                case R.id.tv_balance:
                    showLoading("");
                    bindRomeexPresnterImp.requestExchangeReFlashExchangeFlow();
                    break;
                case R.id.tv_submit:
                    showLoading("");
                    if (etUid.getText().toString().trim().isEmpty()) {
                        dismissLoading();
                        toast(getResources().getString(R.string.text_input_uid));
                        return;
                    }
                    String regex = "^[0-9]*$";
                    if (!Pattern.matches(regex, etUid.getText().toString().trim())) {
                        dismissLoading();
                        toast(getResources().getString(R.string.text_input_uid));
                        return;
                    }
                    if (etInputPhone.getText().toString().trim().isEmpty()) {
                        dismissLoading();
                        toast(getResources().getString(R.string.text_input_phone_num_please));
                        return;
                    }
                    String regex1 = "^[0-9]{11}$";
                    if (!Pattern.matches(regex1, etInputPhone.getText().toString().trim())) {
                        dismissLoading();
                        toast(getResources().getString(R.string.text_input_phone_num_please));
                        return;
                    }
                    bindRomeexPresnterImp.requestFindExchangeInfo(etUid.getText().toString().trim(), etInputPhone.getText().toString().trim());
                    break;

                default:
                    break;
            }
        }
    }

    @Override
    public void success(BindExchangeBean bean) {
        if (bean != null) {
            exchangeUid = bean.getExchangeUid();
            uidQuota = bean.getUidQuota();
            tvBalance.setText(MessageFormat.format("{0}{1}", getResources().getString(R.string.text_exchange_balance), uidQuota));
            bindRomeex();
            if (UserUtils.getCurrentUserExchangeUid().isEmpty()) {
                UserUtils.setCurrentUserExchangeUid(exchangeUid);
            }
        }
        dismissLoading();
    }

    @Override
    public void responseFindExchangeInfoSuccess(FindExchangeInfoBean bean) {
        VerificationCodeDialog verificationCodeDialog = new VerificationCodeDialog(this);
        verificationCodeDialog.fullWidth();
        verificationCodeDialog.setCallback(new VerificationCodeDialog.Callback() {
            @Override
            public void confirm(String verificationCode) {
                verificationCodeDialog.dismiss();
                showLoading("");
                bindRomeexPresnterImp.requestBindExchange(etUid.getText().toString().trim(), bean.getVerifyId(), verificationCode, etInputPhone.getText().toString().trim());

            }

            @Override
            public void dialogDiss() {
            }
        });
        verificationCodeDialog.setCancelable(true);
        verificationCodeDialog.show();
        dismissLoading();
    }

    @Override
    public void responseExchangeFindHomeSuccess(ExchangeFindHomeBean bean) {
        if (bean != null) {
            exchangeUid = bean.getExchangeUid() == null ? "" : bean.getExchangeUid();
            uidQuota = bean.getUidQuota() == null ? "" : bean.getUidQuota();
            tvBalance.setText(MessageFormat.format("{0}{1}", getResources().getString(R.string.text_exchange_balance), uidQuota));
            bindRomeex();
            dismissLoading();
        }
    }

    @Override
    public void ExchangeReFlashExchangeFlow(ExchangeFindHomeBean bean) {
        dismissLoading();
        exchangeUid = bean.getExchangeUid();
        uidQuota = bean.getUidQuota();
        tvBalance.setText(MessageFormat.format("{0}{1}", getResources().getString(R.string.text_exchange_balance), uidQuota));
    }


    @Override
    public void setFailure() {
        dismissLoading();
    }

    private void bindRomeex() {
        if (!exchangeUid.isEmpty()) {
            unbindLayout.setVisibility(View.GONE);
            iconBindedLayout.setVisibility(View.VISIBLE);
            userUid.setText("UID" + exchangeUid);
            //额度记录
//            includeTopTvRight.setText(getResources().getString(R.string.text_exchange_recode_balance));
        }
    }

}
